//
//  AmendBirthdayViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/14.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "AmendBirthdayViewController.h"
#import "ASBirthSelectSheet.h"

@interface AmendBirthdayViewController ()

@property (nonatomic, strong) UILabel *lblShowBirth;
@property (nonatomic, strong) UIButton *changeBirth;


@end

@implementation AmendBirthdayViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改生日";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupConstraints];
    
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
    self.navigationItem.rightBarButtonItem = saveItem;

}

- (void)saveAction {
    
    if (self.lblShowBirth.text.length == 0) {
        ShowMessage(@"请选择您的生日");
        return;
    }
    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Put hostType:MLAHttpHostType_Api api:[self getUserIdApi:@"/api/v1/users"] params:@{@"birthday":self.lblShowBirth.text}.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [UserManager sharedInstance].userModel.value.birthday = self.lblShowBirth.text;
        ShowMessage(@"修改成功");
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^{
        ShowMessage(@"修改失败,请重新修改");
    }];
}


#pragma mark - Constraints
- (void)setupConstraints {
    
    [self.lblShowBirth mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(50);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    
    [self.changeBirth mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(80+50);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    
}

- (UILabel *)lblShowBirth {
    
    if (!_lblShowBirth) {
        _lblShowBirth = [[UILabel alloc] init];
        _lblShowBirth.text = [UserManager sharedInstance].userModel.value.birthday;
        if (_lblShowBirth.text.length == 0) {
            _lblShowBirth.text = @"请选择您的生日";
        }
        _lblShowBirth.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_lblShowBirth];
    }
    
    return _lblShowBirth;
    
}

- (UIButton *)changeBirth {
    
    if (!_changeBirth) {
        _changeBirth = [UIButton buttonWithType:UIButtonTypeCustom];
        [_changeBirth setTitle:@"选择生日" forState:UIControlStateNormal];
        [_changeBirth setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_changeBirth addTarget:self action:@selector(chooseBirthdayAction:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_changeBirth];
    }
    return _changeBirth;
    
}
- (void)chooseBirthdayAction:(id)sender {
    
    ASBirthSelectSheet *datesheet = [[ASBirthSelectSheet alloc] initWithFrame:self.view.bounds];
    datesheet.selectDate = self.lblShowBirth.text;
    datesheet.GetSelectDate = ^(NSString *dateStr) {
        self.lblShowBirth.text = dateStr;
    };
    [self.view addSubview:datesheet];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
